package treasurer;

import home.HomePage;
import includes.Sessions;

import java.io.IOException;
import java.io.Reader;
import java.sql.SQLException;
import java.sql.Timestamp;
import java.util.Date;

import org.apache.wicket.markup.html.WebPage;
import org.apache.wicket.markup.html.form.Button;
import org.apache.wicket.markup.html.form.Form;
import org.apache.wicket.markup.html.form.TextField;
import org.apache.wicket.markup.html.link.BookmarkablePageLink;
import org.apache.wicket.markup.html.panel.FeedbackPanel;
import org.apache.wicket.model.Model;

import com.ibatis.common.resources.Resources;
import com.ibatis.sqlmap.client.SqlMapClient;
import com.ibatis.sqlmap.client.SqlMapClientBuilder;

import controllers.BKSubsidiary;
import controllers.Logs;
import controllers.PE;

public class PropertiesAndEquipment extends WebPage {
	
	Reader reader = Resources.getResourceAsReader("sqlMapConfig.xml");
	SqlMapClient sqlMap = SqlMapClientBuilder.buildSqlMapClient(reader);
	private Model datePurchaseModel = new Model();
	private Model itemModel = new Model();
	private Model costModel = new Model();
	private Model sourceOfFundingModel = new Model();
	private Model propertyNoModel = new Model();
	private Model conditionModel = new Model();
	
	public PropertiesAndEquipment () throws SQLException, IOException
	{
		
		TextField date = new TextField("date", datePurchaseModel);
		date.setRequired(true);
		
		TextField item = new TextField("item", itemModel);
		item.setRequired(true);
		
		TextField cost = new TextField("cost", costModel);
		cost.setRequired(true);
		
		TextField sourceOfFunding = new TextField("sourceOfFunding", sourceOfFundingModel);
		sourceOfFunding.setRequired(true);
		
		TextField propertyNo = new TextField("propertyNo", propertyNoModel);
		propertyNo.setRequired(true);
		
		TextField condition = new TextField("condition", conditionModel);
		condition.setRequired(true);
		
		Form form = new Form("PeForm");
		
		form.add(new Button("add"){
			
			private static final long serialVersionUID = 1L;

			public void onSubmit(){
				
				try {
					String date = (String) datePurchaseModel.getObject();
					String item = (String) itemModel.getObject();
					String cost = (String) costModel.getObject();
					String sourceOfFunding = (String) sourceOfFundingModel.getObject();
					String propertyNo = (String) propertyNoModel.getObject();
					String condition = (String) conditionModel.getObject();
					
					
					String uploader = ((Sessions)getSession()).getDesc();
					
					Date today = new Date();
					Timestamp timestamp = new Timestamp(today.getTime());
					String ts = timestamp.toString();
					
					System.out.println("date: " + date + 
							" items: " + item + 
							" cost: " + cost + 
							" source: " + sourceOfFunding + 
							" property: " + propertyNo + 
							"condition: " + condition +
							"uploader" + uploader +
							"date upload" + ts);
					
					PE pe = new PE();
					pe.setDateOfPurchase(date);
					pe.setItem(item);
					float newCost = Float.valueOf(cost);
					pe.setCost(newCost);
					pe.setSourceOfFunding(sourceOfFunding);
					pe.setPropertyNo(propertyNo);
					pe.setCondition(condition);
					pe.setUploader(uploader);
					pe.setDate_created(ts);
					pe.setStatus("PENDING");
					
					sqlMap.insert("PE.insert", pe);
					
					Logs log = new Logs();
					log.setLog_activity("Properties and Equipments: " + item);
					log.setLog_officer(uploader);
					log.setLog_time(ts);
					
					sqlMap.insert("Logs.insert", log);
				
					PropertiesAndEquipmentList page = new PropertiesAndEquipmentList();
					setResponsePage(page);
					error("added");
			}
				catch (SQLException e){
					 error("Fill up necessary fields");
					 e.printStackTrace();
				  } catch (IOException e) {
					e.printStackTrace();
				}
			}});
		
		add(new BookmarkablePageLink("manageForms", ManageForms.class));
		add(new BookmarkablePageLink("logoutPage", HomePage.class));
		add(new BookmarkablePageLink("home", TreasurerHome.class));
		add(new BookmarkablePageLink("view", PropertiesAndEquipmentList.class));
		
		
		add(new FeedbackPanel("error"));
		
		form.add(date);
		form.add(item);
		form.add(cost);
		form.add(sourceOfFunding);
		form.add(propertyNo);
		form.add(condition);
		add(form);
	}

}
